package com.moxiao.yfjh.module.activity.controller.admin.productInfo;

import com.moxiao.yfjh.module.activity.common.GenActiveNoCommonUtil;
import com.moxiao.yfjh.module.activity.controller.admin.productType.vo.ProductTypeExportReqVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchanType.MerchantTypeDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.merchantInfo.MerchantInfoDO;
import com.moxiao.yfjh.module.activity.dal.dataobject.productType.ProductTypeDO;
import com.moxiao.yfjh.module.activity.service.merchanType.MerchantTypeService;
import com.moxiao.yfjh.module.activity.service.merchantInfo.MerchantInfoService;
import com.moxiao.yfjh.module.activity.service.productType.ProductTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;

import com.moxiao.yfjh.module.activity.controller.admin.productInfo.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.productInfo.ProductInfoDO;
import com.moxiao.yfjh.module.activity.convert.productInfo.ProductInfoConvert;
import com.moxiao.yfjh.module.activity.service.productInfo.ProductInfoService;

@Tag(name = "管理后台 - 相亲商品信息")
@RestController
@RequestMapping("/activity/product-info")
@Validated
public class ProductInfoController {

    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private GenActiveNoCommonUtil genActiveNoCommonUtil;

    @Resource
    private ProductTypeService productTypeService;

    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private MerchantTypeService merchantTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲商品信息")
    @PreAuthorize("@ss.hasPermission('activity:product-info:create')")
    public CommonResult<Long> createProductInfo(@Valid @RequestBody ProductInfoCreateReqVO createReqVO) {
        genActiveNoCommonUtil.addProductInfoNo(createReqVO);
        return success(productInfoService.createProductInfo(createReqVO));
    }

    @GetMapping("/getProductTypeList")
    @Operation(summary = "获得相亲商家类型列表")
    public CommonResult<List<ProductTypeDO>> getProductTypeList() {
        return success(productTypeService.getProductTypeList(new ProductTypeExportReqVO()));
    }


    @PutMapping("/update")
    @Operation(summary = "更新相亲商品信息")
    @PreAuthorize("@ss.hasPermission('activity:product-info:update')")
    public CommonResult<Boolean> updateProductInfo(@Valid @RequestBody ProductInfoUpdateReqVO updateReqVO) {
        productInfoService.updateProductInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲商品信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:product-info:delete')")
    public CommonResult<Boolean> deleteProductInfo(@RequestParam("id") Long id) {
        productInfoService.deleteProductInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲商品信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:product-info:query')")
    public CommonResult<ProductInfoRespVO> getProductInfo(@RequestParam("id") Long id) {
        ProductInfoDO productInfo = productInfoService.getProductInfo(id);
        ProductInfoRespVO productInfoRespVO = ProductInfoConvert.INSTANCE.convert(productInfo);
        if (Objects.nonNull(productInfoRespVO)){
            ProductTypeDO productType = productTypeService.getProductType(productInfo.getProductType());
            if (Objects.nonNull(productType)){
                productInfoRespVO.setProductTypeName(productType.getName());
            }
            MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(productInfoRespVO.getMerchantId());
            if (Objects.nonNull(merchantInfo)){
                productInfoRespVO.setMerchantName(merchantInfo.getName());
            }
        }
        productInfoRespVO.setIsWriteoff(productInfo.getIsWriteoff());
        return success(productInfoRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲商品信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:product-info:query')")
    public CommonResult<List<ProductInfoRespVO>> getProductInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductInfoDO> list = productInfoService.getProductInfoList(ids);
        return success(ProductInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲商品信息分页")
    @PreAuthorize("@ss.hasPermission('activity:product-info:query')")
    public CommonResult<PageResult<ProductInfoRespVO>> getProductInfoPage(@Valid ProductInfoPageReqVO pageVO) {
        PageResult<ProductInfoDO> pageResult = productInfoService.getProductInfoPage(pageVO);
        PageResult<ProductInfoRespVO> voPageResult = ProductInfoConvert.INSTANCE.convertPage(pageResult);
        if (Objects.nonNull(voPageResult)){
            List<ProductInfoRespVO> list = voPageResult.getList();
            if (CollectionUtils.isNotEmpty(list)){
                list.forEach(productInfoRespVO -> {
                        ProductTypeDO productType = productTypeService.getProductType(productInfoRespVO.getProductType());
                        if (Objects.nonNull(productType)){
                            productInfoRespVO.setProductTypeName(productType.getName());
                        }
                        MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(productInfoRespVO.getMerchantId());
                        if (Objects.nonNull(merchantInfo)){
                            productInfoRespVO.setMerchantName(merchantInfo.getName());
                        }
                });
            }
        }
        return success(voPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲商品信息 Excel")
    @PreAuthorize("@ss.hasPermission('activity:product-info:export')")
    @OperateLog(type = EXPORT)
    public void exportProductInfoExcel(@Valid ProductInfoExportReqVO exportReqVO,
                                       HttpServletResponse response) throws IOException {
        List<ProductInfoDO> list = productInfoService.getProductInfoList(exportReqVO);
        // 导出 Excel
        List<ProductInfoExcelVO> datas = ProductInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲商品信息.xls", "数据", ProductInfoExcelVO.class, datas);
    }


    @GetMapping("/getProductInfoSelect")
    @Operation(summary = "获得相亲商品下拉列表")
    public CommonResult<List<ProductInfoRespVO>> getProductInfoSelect(@Valid ProductInfoPageReqVO pageVO) {
        ProductInfoExportReqVO exportReqVO = new ProductInfoExportReqVO();
        List<ProductInfoDO> list = productInfoService.getProductInfoList(exportReqVO);
        List<ProductInfoRespVO> datas = ProductInfoConvert.INSTANCE.convertList(list);
        if (Objects.nonNull(datas)) {
            if (CollectionUtils.isNotEmpty(datas)) {
                datas.forEach(productInfoRespVO -> {
                    ProductTypeDO productType = productTypeService.getProductType(productInfoRespVO.getProductType());
                    if (Objects.nonNull(productType)) {
                        productInfoRespVO.setProductTypeName(productType.getName());
                    }
                    MerchantInfoDO merchantInfo = merchantInfoService.getMerchantInfo(productInfoRespVO.getMerchantId());
                    if (Objects.nonNull(merchantInfo)) {
                        productInfoRespVO.setMerchantName(merchantInfo.getName());
                    }
                    MerchantTypeDO merchantTypeDO = merchantTypeService.getMerchantType(merchantInfo.getMerchantType());
                    if (Objects.nonNull(merchantTypeDO)) {
                        productInfoRespVO.setMerchantTypeName(merchantTypeDO.getName());
                    }
                    productInfoRespVO.setProductId(productInfoRespVO.getId());
                });
            }
        }
        return success(datas);
    }
}
